import base from "@/config/baseUrl";
import store from '@/store';
import {postImgCheck,getQiNiuToken} from '@/config/api.js';
/* 
* 选择图片
* count:上传图片数量
* sourceType: 1.从相册选图，2.使用相机 3.二者都有
* callback:回调
*/
function chooseImage(callback,count=9,sourceType=3) {
	let that = this
	var sourceType = ['album', 'camera']
	if(sourceType==1){
		sourceType = ['album']
	}else if(sourceType==2){
		sourceType = ['camera']
	}
	uni.chooseImage({
		count: count,  // 最多可以选择的图片张数，默认9
		sizeType: ['compressed'], // original 原图，compressed 压缩图，默认二者都有
		sourceType: sourceType, // album 从相册选图，camera 使用相机，默认二者都有
		success: function (res) {
			console.log(res,'chooseImage')
			callback && callback(res)
			// 上传图片用到res.tempFilePaths,这个是列表多个图需循环上传
		}
	})
}

/* 
* 选择视频
* sourceType: 1.从相册选视频，2.使用相机拍摄 3.二者都有
* callback:回调
*/
function chooseVideo(callback,sourceType=3) {
	let that = this
	var sourceType = ['album', 'camera']
	if(sourceType==1){
		sourceType = ['album']
	}else if(sourceType==2){
		sourceType = ['camera']
	}
	uni.chooseVideo({
		sourceType: sourceType, // album 从相册选视频，camera 使用相机拍摄，默认为：['album', 'camera']
		success: function (res) {
			console.log(res,'chooseVideo')
			callback && callback(res)
			// 上传视频用到res.tempFilePath
		}
	})
}

/* 
* 选择文件
* count:上传数量
* callback:回调
*/
function chooseFile(callback,count=1) {
	// #ifdef MP-WEIXIN
	wx.chooseMessageFile({
	    count: count,
	    success: function (res) {
	    	console.log(res,'chooseFile');
	    	callback && callback(res)
	    }
	})
	// #endif
	// #ifdef H5
	// 需要hx2.9.9以上才支持uni.chooseFile
	uni.chooseFile({
	    count: count,
	    success: function (res) {
	    	console.log(res,'chooseFile');
	    	callback && callback(res)
	    }
	})
	// #endif
}





/* 
* 文件上传
* filePath:临时上传路径
* type：1.图片上传、2.视频上传 3.文件上传
* formData：其他参数
*/
function uploadFilePromise(filePath,fileType) {
	return getQiNiuToken({custom:{load:false}}).then((data) => {
	  let uploadUrl = 'https://upload.qiniup.com';
	  return new Promise((resolve, reject) => {
		   let timestamp=new Date().getTime();
		   var result =/\.[^\.]+/.exec(filePath);
		   // console.log("result",result)
		   let fileName = timestamp + result
		   if(fileType){
			   // #ifdef H5
			   fileName = timestamp + result+fileType
			   // #endif
		   }
		   // console.log("fileName2",fileName)
		  uni.uploadFile({
		  		url: uploadUrl,
		  		name: 'file',
		  		filePath: filePath,
		  		formData: {
		  		  token: data.token,
				  key:fileName
		  		},
		  		success: function (res) {
		  		  let key = JSON.parse(res.data).key;
				  console.log("data.domain + key",data.domain + key)
				  resolve(data.domain + key)
		  		},
		  		fail: function (res) {
		  		  uni.$u.toast("上传失败")
		  		},
		  })
	  }) 
	})
}

//图片是否违规检测
function imgCheck(filePath,fileType) {
	return uploadFilePromise(filePath,fileType).then((img)=>{
		return postImgCheck({picUrl:img}).then((res)=>{
			if(res.checkResult == 1){
				return img;
			}else{
				uni.$u.toast("图片违规")
				return '';
			}
		})
	})
}




export {
	chooseImage,       //选择图片
	chooseVideo,       //选择视频
	chooseFile,        //选择文件
	uploadFilePromise, //上传
	imgCheck,//上传图片并检测
}